/**
 * 全屏翻页闹钟应用的主Activity
 * 
 * 功能：
 * 1. 显示全屏数字时钟（12小时制）
 * 2. 提供闹钟设置页面
 * 3. 支持左右滑动翻页
 * 4. 保持屏幕常亮
 */
package cn.uncle.shizhong

import android.os.Bundle
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectHorizontalDragGestures
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.Alignment
import androidx.compose.ui.platform.LocalConfiguration
import cn.uncle.shizhong.ui.theme.ShiZhong2Theme
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import kotlinx.coroutines.delay

class MainActivity : ComponentActivity() {
    /**
     * Activity创建时初始化
     * @param savedInstanceState 保存的状态数据
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 设置全屏并保持屏幕常亮
        window.addFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN or
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
        )
        
        setContent {
            ShiZhong2Theme {
                // 当前页面索引：0=时钟页面，1=闹钟页面
                var currentPage by remember { mutableStateOf(0) }
                // 当前时间状态
                var currentTime by remember { mutableStateOf(LocalTime.now()) }
                
                // 每秒更新时间
                LaunchedEffect(Unit) {
                    while (true) {
                        delay(1000)
                        currentTime = LocalTime.now()
                    }
                }

                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .background(Color.Black)
                        .pointerInput(Unit) {
                            detectHorizontalDragGestures { change, dragAmount ->
                                // 左右滑动切换页面
                                if (dragAmount > 0) currentPage = (currentPage + 1) % 2
                                else currentPage = (currentPage - 1 + 2) % 2
                            }
                        }
                ) {
                    when (currentPage) {
                        0 -> ClockPage(currentTime)
                        1 -> AlarmPage()
                    }
                }
            }
        }
    }
}

/**
 * 时钟页面组件
 * @param time 当前时间
 */
@Composable
fun ClockPage(time: LocalTime) {
    // 格式化时间：小时、分钟、AM/PM
    var hour = remember(time) { DateTimeFormatter.ofPattern("h").format(time) }
    val minute = remember(time) { DateTimeFormatter.ofPattern("mm").format(time) }
    val ampm = remember(time) { DateTimeFormatter.ofPattern("a").format(time) }
    
    // 根据屏幕方向调整字体大小
    val configuration = LocalConfiguration.current
    val isLandscape = configuration.screenWidthDp > configuration.screenHeightDp
    val fontSize = if (isLandscape) {
        (configuration.screenHeightDp * 0.80).sp  // 横屏时字体更大
    } else {
        72.sp  // 竖屏默认大小
    }

    // 确保小时数对齐（1位数补空格）
    if (hour.length < 2) {
        hour = " $hour"
    }

    // 使用LCD数字字体
    val digitalFont = FontFamily(
        Font(R.font.digital_7)
    )

    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        // 主时钟显示
        Text(
            text = "$hour $minute",
            style = MaterialTheme.typography.displayLarge.copy(
                fontFamily = digitalFont
            ),
            fontWeight = FontWeight.Bold,
            fontSize = fontSize,
            color = Color.White
        )
        
        // AM/PM显示在左下角
        Text(
            text = ampm,
            style = MaterialTheme.typography.bodySmall,
            modifier = Modifier
                .align(Alignment.BottomStart)
                .padding(16.dp),
            color = Color.White
        )
    }
}

/**
 * 闹钟设置页面组件
 */
@Composable
fun AlarmPage() {
    // 闹钟时间状态
    var alarmTime by remember { mutableStateOf(LocalTime.of(7, 0)) }
    
    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // 标题
        Text(
            text = "Set Alarm",
            style = MaterialTheme.typography.headlineLarge,
            modifier = Modifier.padding(bottom = 32.dp),
            color = Color.White
        )
        
        // 闹钟时间显示
        Text(
            text = DateTimeFormatter.ofPattern("HH:mm").format(alarmTime),
            style = MaterialTheme.typography.displayMedium,
            color = Color.White
        )
    }
}
